package com.light.gamecomponents.physics;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.light.gamecomponents.gravitylight.GravityLight;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class RedLight extends PhysicsComponents implements ContactListener{
	Sprite m_sprite2;
	Sprite Black;
	float timer = 0;
	float rotation = 0;
	boolean inLight = false;
	private LoopEntityModifier m_nowModifiler;
	//= 	
	public RedLight(Roller game, Vector2 initialPosition, float rotation) 
	{
		super(game, null, initialPosition, new Vector2(0,0));
		this.rotation = rotation;
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		if(inLight)
			timer += pSecondsElapsed;
		
		if (timer >= 0.4f)
			timer -= 0.4f;
		m_sprite2.setScale((float)(1.0f + 0.3f * Math.sin(timer / 0.4 * 3.1416) * 2.0f));
	}

	@Override
	public void reset() {
		
	}
	
	@Override
	public void stopBody() 
	{ 
		inLight = false;
	}
	
	@Override
	public void startBody() 
	{ 
		inLight = true;
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources("RedLight", m_game);
		ResourceManager.onLoadResources("RedLightGlow", m_game);

	}
	
	@Override
	public void unLoadResources() {
		
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) {
		
		m_sprite = new Sprite(m_initialPosition.x, 
				  		      m_initialPosition.y,
				  		      ResourceManager.getTextureRegion("RedLight"));
		
		m_sprite2 = new Sprite(m_initialPosition.x, 
				   				m_initialPosition.y,
				   				ResourceManager.getTextureRegion("RedLightGlow"));
		
		float tempX = - 58;
		float tempY = - 56;

		m_sprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_sprite2.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		scene.attachChild(m_sprite);
		scene.getLastChild().attachChild(m_sprite2);
		m_sprite2.setPosition(tempX, tempY);
		m_sprite2.setScaleCenter(m_sprite2.getWidth() / 2, m_sprite2.getHeight() / 2);
		m_sprite.setScale(0.5f);
		m_sprite2.setScale(1.0f * 2.0f);
		m_sprite2.setAlpha(0.3f);
		m_sprite.setRotation(rotation);
		((RollerPhysicsWorld)physicsWorld).registerUpdateHandler(this);
				
	}	
	
	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) 
	{
	}

	@Override
	public void onLoadEngine() {
		
	}

	@Override
	public void setPosition(int x, int y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beginContact(Contact contact) {

	}

	@Override
	public void endContact(Contact contact) {
		// TODO Auto-generated method stub
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}	
	
	@Override
	public boolean isInLight(GravityLight light)
	{
		Vector2 disVec = new Vector2(getSpritePosition().x - light.getSpritePosition().x, 
				 					 getSpritePosition().y - light.getSpritePosition().y);

		float dis = (float)Math.sqrt(disVec.x * disVec.x + disVec.y * disVec.y);
		float minDis = m_sprite.getWidth() / 2 + light.getWidth() / 2 - 15;
		
		if (dis <= minDis)
			return true;
		else
			return false;
		
	}
}
